package org.xx.armory.swing.components.verifiers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.Color;

public class StyledInputComponentVerificationAdapter
        extends InputComponentVerificationAdapter {
    @SuppressWarnings("unused")
    private Logger logger = LoggerFactory.getLogger(StyledInputComponentVerificationAdapter.class);

    public StyledInputComponentVerificationAdapter(
            JComponent component,
            TextVerifier verifier
    ) {
        super(component, verifier);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void statusReset() {
        super.statusReset();

        // 将背景色恢复为默认。
        final Color background = UIManager.getColor("TextField.background");
        getComponent().setOpaque(true);
        getComponent().setBackground(background);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void statusChanged(
            VerifiedStatus status
    ) {
        super.statusChanged(status);

        // 根据检测结果设置背景色。
        final boolean opaque;
        final Color background;
        switch (status) {
            case OK:
                // background = new Color(0xc4df9b);
                opaque = false;
                background = UIManager.getColor("TextField.background");
                break;
            case UNKNOWN:
                opaque = true;
                background = new Color(0xfff79a);
                break;
            case REQUIRED_ERROR:
            case FORMAT_ERROR:
            case TOO_SMALL_ERROR:
            case TOO_LARGE_ERROR:
                opaque = true;
                background = new Color(0xf6989d);
                break;
            default:
                return;
        }

        getComponent().setOpaque(opaque);
        getComponent().setBackground(background);
    }
}
